package com.dongdongshop.service.impl.user;

import com.dongdongshop.mapper.TbUserMapper;
import com.dongdongshop.pojo.admin.TbUser;
import com.dongdongshop.service.user.UserService;
import com.dongdongshop.util.IdWorker;
import com.dongdongshop.util.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;

@Service
@com.alibaba.dubbo.config.annotation.Service
public class UserServiceImpl implements UserService {
    @Autowired
    private TbUserMapper tbUserMapper;

    @Override
    public TbUser getUserByUsername(String username) {
        return tbUserMapper.getUserByUsername(username);
    }

    @Override
    public int insertUser(TbUser tbUser) {
        tbUser.setCreated(new Date());
        tbUser.setUpdated(new Date());
        String salt = ShiroUtils.generateSalt(6);
        String password = ShiroUtils.encryptPassword("MD5", tbUser.getPassword(), salt, 3);
        tbUser.setPassword(password);
        tbUser.setSalt(salt);
        return tbUserMapper.insertSelective(tbUser);
    }

    @Override
    public TbUser forgotPassword(String username) {
        String p = "1234";
        TbUser user = tbUserMapper.getUserByUsername(username);
        String salt = ShiroUtils.generateSalt(6);
        String password = ShiroUtils.encryptPassword("MD5", p, salt, 3);
        user.setSalt(salt);
        user.setPassword(password);
        tbUserMapper.updateByPrimaryKeySelective(user);
        user.setPassword(p);
        return user;
    }
}
